package org.teamh.connection.json.handler;

import org.teamh.connection.json.event.JsonEventObject;
import org.teamh.game.Start;
import org.teamh.gui.Event;
import org.teamh.logic.datamodel.*;

public class TileHandler extends JsonMasterHandler {

	public TileHandler(Start start, DatamodelManager manager) {
		super(start, manager);
	}
	
	public boolean handleMsg(JsonEventObject json) {
		
		if (json.getSource().startsWith("Tile")) {
			if (json.getProperty().equals("game")) {
				if(json.getOldValue() != null && json.getNewValue() == null) {
					start.getGui().addEvent(new Event("closed", ""));
				} else {
					manager.getTile(json.getSource()).setGame(start.getLogic().getGame());
				}
				return true;
			} else if (json.getProperty().equals("map")) {
				Map map = manager.getMap(json.getNewValue());
				if(map == null) {
					map = new Map(json.getNewValue());
					manager.addMap(map);
				}
				manager.getTile(json.getSource()).setMap(map);
				return true;
			} else if (json.getProperty().equals("position")) {
				manager.getTile(json.getSource()).setPosition(json.getNewValue());
				return true;
			} else if (json.getProperty().equals("userAssets")) {
				if (json.getNewValue() != null) {
					manager.getTile(json.getSource()).setUserAssets(manager.getUserAssets(json.getNewValue()));
					start.getGui().addEvent(new Event("tile", manager.getTile(json.getSource()).getPosition() + " " + manager.getTile(json.getSource()).getID()));
				}
				return true;
			}
			return false;
		} else {
			return false;
		}	
	}
}